<template>
  <div class="biangeng">
    <div>
      <selectPicker
        :isTime="false"
        :values="'DICNAME'"
        :DICID="'DICID'"
        :columns="JTYWData"
        :selectValue="JTYW | inUseName(JTYWData)"
        :placeholder="'请选择具体业务'"
        :labels="'具体业务'"
        @getMessage="setValue">
      </selectPicker>
    </div>
    <van-tabs v-model="active">
      <van-tab title="历史审批信息">
        <xiangqing style="height: 90%" />
      </van-tab>
      <van-tab title="附件材料">
        <div class="middle">
          <h3>申报材料</h3>
          <div class="p"><span style="color: red">*</span>1.乡村建设规划许可证</div>
          <div style="height: 150px; line-height: 150px; text-align: left">
            <upLoad :rowData="rowData" :ISDelete="true" :FJData="FJData4" :SHITUID="'98bcf528966e48ce896a8b3fd02d45d5'" :UpLoadID="'7a3fc061c866434b8475d3775c9ddcea'" />
          </div>
          <div class="p"><span style="color: red">*</span>2.农村宅基地批准书</div>
          <div style="height: 150px; line-height: 150px; text-align: left">
            <upLoad :rowData="rowData" :ISDelete="true" :FJData="FJData6" :SHITUID="'98bcf528966e48ce896a8b3fd02d45d5'" :UpLoadID="'7a3fc061c866434b8475d3775c9ddcea'" />
          </div>
          <div class="p"><span style="color: red">*</span>3.建房施工合同</div>
          <div style="height: 150px; line-height: 150px; text-align: left">
            <upLoad :rowData="rowData" :ISDelete="true" :FJData="FJData5" :SHITUID="'98bcf528966e48ce896a8b3fd02d45d5'" :UpLoadID="'7a3fc061c866434b8475d3775c9ddcea'" />
          </div>
        </div>
      </van-tab>
      <van-tab :title="titles">
        <div style="height: 80vh; overflow: auto">
          <div class="middle" v-if="JTYW == '2'">
            <div v-for="(item, index) in tableList" :key="index">
              <h2 v-if="item.title" style="height: 50px; line-height: 50px">
                {{ item.title }}
                <van-button style="float: right; margin-top: 16px" @click="handlegotoGJ1(item.title)" v-show="item.title == '施工单位'" size="small" type="info">添加</van-button>
                <van-button style="float: right; margin-top: 16px" @click="handlegotoGJ1(item.title)" v-show="item.title == '施工工匠'" size="small" type="info">添加</van-button>
              </h2>
              <!-- 输入框  isFiled-->
              <div v-else-if="item.isFiled" style="display: flex; justify-content: center; align-items: center; color: black; border-bottom: 1px solid #d2d2d2">
                <van-field :required="item.isread" :readonly="item.isreadonly" v-model="JFData[item.key]" :label="item.label" :placeholder="'请输入' + item.label" />
              </div>
              <!--下拉框 selectPicker-->
              <div v-else-if="item.isSelect">
                <div v-if="!item.isTime">
                  <selectPicker
                    :bitian="item.isread"
                    :isTime="item.isTime"
                    :values="'DICNAME'"
                    :DICID="'DICID'"
                    placeholder="请选择"
                    :columns="item.NYDARR"
                    :labels="item.label"
                    :selectValue="JFData[item.key] | inUseName(item.NYDARR)"
                    @getMessage="setValue">
                  </selectPicker>
                </div>
                <div v-else>
                  <selectPicker :bitian="item.isread" :isTime="item.isTime" placeholder="请选择1" :labels="item.label" :selectValue="JFData[item.key]" @getMessage="setValue"> </selectPicker>
                </div>
              </div>
            </div>

            <h2><span style="color: red">*</span>变更原因</h2>
            <div>
              <van-field v-model="JFData.BGYY" type="textarea" label :border="false" disable-default-padding style="background-color: #e4e4e4; margin-top: 8px; min-height: 140px" />
            </div>
          </div>
          <div class="middle" v-if="JTYW == '3'">
            <div v-for="(item, index) in tableList1" :key="index">
              <h2 v-if="item.title" style="height: 50px; line-height: 50px">
                {{ item.title }}
                <van-button style="float: right; margin-top: 16px" @click="handlegotoGJ" v-show="item.title == '施工单位'" size="mini" type="info">添加</van-button>
              </h2>
              <!-- 输入框  isFiled-->
              <div v-else-if="item.isFiled" style="display: flex; justify-content: center; align-items: center; color: black; border-bottom: 1px solid #d2d2d2">
                <van-field :required="item.isread" :readonly="item.isreadonly" v-model="JFData[item.key]" :label="item.label" :placeholder="'请输入' + item.label" />
              </div>
              <!--下拉框 selectPicker-->
              <div v-else-if="item.isSelect">
                <div v-if="!item.isTime">
                  <selectPicker
                    :bitian="item.isread"
                    :isTime="item.isTime"
                    :values="'DICNAME'"
                    :DICID="'DICID'"
                    placeholder="请选择"
                    :columns="item.NYDARR"
                    :labels="item.label"
                    :selectValue="JFData[item.key]"
                    @getMessage="setValue">
                  </selectPicker>
                </div>
                <div v-else>
                  <selectPicker :bitian="item.isread" :isTime="item.isTime" placeholder="请选择" :labels="item.label" :selectValue="JFData[item.key]" @getMessage="setValue"> </selectPicker>
                </div>
              </div>
            </div>
          </div>
          <div class="middle" v-if="JTYW == '4'">
            <div v-for="(item, index) in tableList3" :key="index">
              <h2 v-if="item.title" style="height: 50px; line-height: 50px">{{ item.title }}</h2>
              <!-- 输入框  isFiled-->
              <div v-else-if="item.isFiled" style="display: flex; justify-content: center; align-items: center; color: black; border-bottom: 1px solid #d2d2d2">
                <van-field :required="item.isread" :readonly="item.isreadonly" v-model="JFData[item.key]" :label="item.label" :placeholder="'请输入' + item.label" />
              </div>
              <!--下拉框 selectPicker-->
              <div v-else-if="item.isSelect">
                <div v-if="!item.isTime">
                  <selectPicker
                    :bitian="item.isread"
                    :isTime="item.isTime"
                    :values="'DICNAME'"
                    :DICID="'DICID'"
                    placeholder="请选择"
                    :columns="item.NYDARR"
                    :labels="item.label"
                    :selectValue="JFData[item.key] | inUseName(item.NYDARR)"
                    @getMessage="setValue">
                  </selectPicker>
                </div>
                <div v-else>
                  <selectPicker :bitian="item.isread" :isTime="item.isTime" placeholder="请选择" :labels="item.label" :selectValue="JFData[item.key]" @getMessage="setValue"> </selectPicker>
                </div>
              </div>
            </div>
          </div>
          <div class="middle" v-if="JTYW == '5'">
            <div v-for="(item, index) in tableList2" :key="index">
              <h2 v-if="item.title" style="height: 50px; line-height: 50px">{{ item.title }}</h2>
              <!-- 输入框  isFiled-->
              <div v-if="item.isFiled && item.label == '延期次数'" style="display: flex; justify-content: center; align-items: center; color: black; border-bottom: 1px solid #d2d2d2">
                <van-field :required="item.isread" :readonly="item.isreadonly" v-model="JFData[item.key]" :label="item.label" :placeholder="'请输入' + item.label" />
              </div>
              <div
                v-if="item.isFiled && !JFData['YQCS'] && item.label !== '延期次数' && item.label !== '延期事由2'"
                style="display: flex; justify-content: center; align-items: center; color: black; border-bottom: 1px solid #d2d2d2">
                <van-field :required="item.isread" :readonly="item.isreadonly" v-model="JFData[item.key]" :label="item.label" :placeholder="'请输入' + item.label" />
              </div>
              <div
                v-else-if="item.isFiled && JFData['YQCS'] && JFData['YQCS'] == 1 && item.label !== '延期次数'&& item.label !== '延期事由2'"
                style="display: flex; justify-content: center; align-items: center; color: black; border-bottom: 1px solid #d2d2d2">
                <van-field :required="item.isread" :readonly="!item.isreadonly" v-model="JFData[item.key]" :label="item.label" :placeholder="'请输入' + item.label" />
              </div>
              <div
                v-else-if="item.isFiled && JFData['YQCS'] && JFData['YQCS'] == 2 && item.label == '延期事由2'"
                style="display: flex; justify-content: center; align-items: center; color: black; border-bottom: 1px solid #d2d2d2">
                <van-field :required="item.isread" :readonly="!item.isreadonly" v-model="JFData[item.key]" :label="item.label" :placeholder="'请输入' + item.label" />
              </div>
              <!--下拉框 selectPicker-->
              <div v-else-if="item.isSelect">
                <div v-if="!item.isTime">
                  <selectPicker
                    :bitian="item.isread"
                    :isTime="item.isTime"
                    :values="'DICNAME'"
                    :DICID="'DICID'"
                    placeholder="请选择"
                    :columns="item.NYDARR"
                    :labels="item.label"
                    :selectValue="JFData[item.key] | inUseName(item.NYDARR)"
                    @getMessage="setValue">
                  </selectPicker>
                </div>
                <div v-else>
                  <selectPicker :bitian="item.isread" :isTime="item.isTime" placeholder="请选择" :labels="item.label" :selectValue="JFData[item.key]" @getMessage="setValue"> </selectPicker>
                </div>
              </div>
            </div>
            <!-- <h2><span style="color: red">*</span>申请报告</h2>
            <div style="height: 150px; line-height: 150px; text-align: left">
              <upLoad :rowData="rowData" :FJData="FJData4" :SHITUID="'98bcf528966e48ce896a8b3fd02d45d5'" :UpLoadID="'7a3fc061c866434b8475d3775c9ddcea'" />
            </div> -->
          </div>
        </div>
        <div class="footer">
          <div>
            <van-button @click="saveData" style="width: 90vw" type="info">提交</van-button>
          </div>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import upLoad from '@/utils/upLoad'
import selectPicker from '@/utils/selectPicker'
import xiangqing from './xiangqing.vue'
import { FileServerProxy, houseRemoldUnitProjectPersonXM } from '@/utils/api'
import { GSA_Add1 } from '@/common/commonApi'
import { GJDBFunction2 } from '@/utils/queryData'
import { getThirdCompanySettled } from '../../../utils/api'
export default {
  computed: {
    titles: {
      get() {
        if (this.JTYW == '3') {
          return '中止信息'
        } else if (this.JTYW == '4') {
          return '恢复信息'
        } else if (this.JTYW == '5') {
          return '延期信息'
        } else {
          return '变更信息'
        }
      },
      set() {}
    }
  },
  data() {
    return {
      active: 0,
      JTYW: '',
      JFData: {},
      JTYWData: [
        {
          DICNAME: '变更',
          DICID: '2'
        },
        {
          DICNAME: '延期',
          DICID: '5'
        }
      ],
      rowData: {},
      tableList: [
        {
          title: '建房户主信息'
        },
        {
          label: '是否变更',
          key: 'IF_BG',
          isread: true,
          isSelect: true,
          NYDARR: [
            {
              DICNAME: '是',
              DICID: '1'
            },
            {
              DICNAME: '否',
              DICID: '0'
            }
          ],
          isreadonly: false,
          isTime: false
        },
        {
          label: '户主姓名',
          key: 'BG_HZXM',
          isFiled: true,
          isread: true,
          isreadonly: false
        },
        {
          label: '户主身份证号',
          key: 'BG_SFZH',
          isFiled: true,
          isread: true,
          isreadonly: false
        },
        {
          label: '联系电话',
          key: 'BG_LXDH',
          isFiled: true,
          isread: true,
          isreadonly: false
        },
        {
          title: '监理单位'
        },
        {
          label: '监理单位名称',
          key: 'JLDWMC',
          isFiled: true,
          isread: true,
          isreadonly: false
        },
        {
          label: '监理单位负责人',
          key: 'JLDW_SGFZR',
          isFiled: true,
          isread: true,
          isreadonly: false
        }
      ],
      tableList1: [
        {
          title: '申请理由'
        },
        {
          label: '申请事由',
          key: 'ZZSQSY',
          isFiled: true,
          isread: true,
          isreadonly: false
        },
        {
          label: '中止施工时间',
          key: 'ZZSGSJ',
          isread: false,
          isSelect: true,
          isreadonly: false,
          isTime: true
        },
        {
          label: '中止施工原因',
          key: 'ZZSGYY',
          isFiled: true,
          isread: true,
          isreadonly: false
        },
        {
          label: '在施部位',
          key: 'ZSBW',
          isFiled: true,
          isread: true,
          isreadonly: false
        },
        {
          label: '维修管理措施',
          key: 'WXGLCS',
          isFiled: true,
          isread: true,
          isreadonly: false
        }
      ],
      FJData1: [
        {
          title: '',
          groupGuid: '依法办理用地批准手续的证明文件',
          FileName: '无',
          fileList: []
        }
      ],
      tableList2: [
        {
          title: '申请理由'
        },
        {
          label: '延期次数',
          key: 'YQCS',
          isFiled: true,
          isread: true,
          isreadonly: true
        },
        // {
        //   label: '延期时限',
        //   key: 'YQSX',
        //   isread: true,
        //   isSelect: true,
        //   isreadonly: false,
        //   isTime: true
        // },
        {
          label: '延期事由',
          key: 'YQSY',
          isFiled: true,
          isread: true,
          isreadonly: true
        },
        {
          label: '延期事由2',
          key: 'YQSY2',
          isFiled: true,
          isread: true,
          isreadonly: true
        }
      ],
      tableList3: [
        {
          title: '申请理由'
        },
        {
          label: '申请事由',
          key: 'HFSQSY',
          isFiled: true,
          isread: true,
          isreadonly: false
        },
        {
          label: '恢复时间',
          key: 'HFSJ',
          isread: true,
          isSelect: true,
          isreadonly: false,
          isTime: true
        }
      ],
      FJData2: [
        {
          title: '',
          groupGuid: '施工图设计文件审查合格书',
          FileName: '无',
          fileList: []
        }
      ],
      FJData4: [
        {
          title: '',
          groupGuid: 'e44e3da4-dc5a-434e-b682-033bf47ff1f4',
          FileName: '无',
          fileList: []
        }
      ],
      FJData5: [
        {
          title: '',
          groupGuid: '2774cbb5-1854-4a0a-afaf-bee2e8f83cab',
          FileName: '无',
          fileList: []
        }
      ],
      FJData6: [
        {
          title: '',
          groupGuid: '618bd7e6-cd39-46c2-b0f7-56eef4bbb456',
          FileName: '无',
          fileList: []
        }
      ],
      rowData: {}
    }
  },
  created() {
    this.rowData = JSON.parse(window.sessionStorage.getItem('fileData'))
    this.getData()
    this.JTYW = window.sessionStorage.getItem('RUNTYPE')
    if(window.sessionStorage.getItem('ISBian') == 'true'){
        this.JTYW = 2
    }
  },
  filters: {
    inUseName: function (msg, data) {
      let name = ''
      data.forEach((item) => {
        if (item.DICID == msg) {
          name = item.DICNAME
        }
      })
      return name
    }
  },
  methods: {
    setValue(name, value, lable) {
      if (lable == '是否变更') {
        this.JFData.IF_BG = value
      } else if (value == '恢复时间') {
        this.JFData.HFSJ = name
      } else if (value == '中止施工时间') {
        this.JFData.ZZSGSJ = name
      } else if (value == '延期时限') {
        this.JFData.YQSX = name
      } else if (lable == '项目负责人') {
        this.JFData.BG_SGDWXMFZR = name
      }
    },
    async saveData() {
      //需要判断当前的延期次数
      let code = window.localStorage.getItem('citycodeChange')
      window.localStorage.setItem('citycode',code)
      if (this.JFData.YQCS == 3 && this.JTYW == '5') {
        this.$toast.fail('已进行两次延期，不可再次申请延期')
        return
      }
      //先更新数据
      this.update()
      this.updataQHT()
    },
    saveDatas: async function () {
      console.log(this.JFData, 'this.JFData')
      if (this.JFData.OPTYPE == 59) {
        this.tableList.push(
          {
            title: '施工工匠'
          },
          {
            label: '工匠名称',
            key: 'BG_SGDWQYMC',
            isFiled: true,
            isread: false,
            isreadonly: false
          },
          {
            label: '工匠负责人',
            key: 'BG_SGDWXMFZR',
            isFiled: true,
            isread: false,
            isreadonly: false
          }
        )
      } else {
        this.tableList.push(
          {
            title: '施工单位'
          },
          {
            label: '企业名称',
            key: 'BG_SGDWQYMC',
            isFiled: true,
            isread: false,
            isreadonly: false
          },
          {
            label: '统一社会信用代码',
            key: 'BG_SGDWXYDM',
            isFiled: true,
            isread: false,
            isreadonly: false
          },
          {
            label: '项目负责人',
            key: 'BG_SGDWXMFZR',
            isread: false,
            NYDARR: [],
            isSelect: true,
            isreadonly: false,
            isTime: false
          }
        )
      }
    },
    //更新数据
    async update() {
      let newobj = {}
      if (window.sessionStorage.getItem('RUNTYPE') == '2' || this.JTYW == '2') {
        newobj.PKID = this.JFData.PKID
        newobj.IF_BG = this.JFData.IF_BG
        newobj.BG_SGDWQYMC = this.JFData.BG_SGDWQYMC
        newobj.BG_SGDWXYDM = this.JFData.BG_SGDWXYDM
        newobj.BG_SGDWXMFZR = this.JFData.BG_SGDWXMFZR
        newobj.BGYY = this.JFData.BGYY
        newobj.BG_HZXM = this.JFData.BG_HZXM
        newobj.BG_SFZH = this.JFData.BG_SFZH
        newobj.BG_LXDH = this.JFData.BG_LXDH
        newobj.BG_JLDWMC = this.JFData.JLDWMC
        newobj.BG_JLDW_SGFZR = this.JFData.JLDW_SGFZR
      } else if(this.JTYW == '5'){
        newobj.PKID = this.JFData.PKID
        newobj.YQCS = this.JFData.YQCS
        newobj.YQSY = this.JFData.YQSY
        newobj.YQSY2 = this.JFData.YQSY2
      } else {
        newobj.PKID = this.JFData.PKID
        newobj.HFSQSY = this.JFData.HFSQSY
        newobj.HFSJ = this.JFData.HFSJ
      }
      GSA_Add1('4929e1f4-3884-4f8f-8d5d-966053e87437', false, newobj)
    },
    //重新签合同
    async updataQHT() {
      if (window.sessionStorage.getItem('ISBian') == 'true') {
       
      } else {
        let data = [{ I_ROW_GUID: this.rowData.ROW_GUID }]
        const res = await GJDBFunction2('htqxqd', data)
        //如果是变更    RUNTYPE == 2 需要先签合同 签完合同才能发走
        //先判断当前是否更换了工匠或者建筑企业
        this.$router.push({
          path: '/TreeSelect',
          query: {
            data: window.sessionStorage.getItem('fileData'),
            SJXK: window.sessionStorage.getItem('RUNTYPE')
          }
        })
      }
    },
    //获取项目负责人接口
    async getProgectData() {
      let params = {
        isDelete: 0,
        uscc: this.JFData.BG_SGDWXYDM
      }
      const res = await houseRemoldUnitProjectPersonXM(params)
      this.tableList.forEach((item) => {
        if (item.label == '项目负责人') {
          res.data.data.forEach((item1) => {
            item.NYDARR.push({
              DICNAME: item1.nickname,
              DICID: item1.idCard
            })
          })
          console.log(item.NYDARR, 'item.NYDARR')
        }
      })
    },
    //回显数据
    async getData() {
      let data = {
        accessCode: '688bd3883572440a859dfd8ba9194e7b',
        cityCode: window.localStorage.getItem('citycodeChange') ? window.localStorage.getItem('citycodeChange') : '441300000000',
        params: {
          dataguid: window.localStorage.getItem('newData')?"f0ed818a-6897-4a9a-96b8-e721afd0249e":"3bc9768e-4a52-48b3-aabe-90c62410bfb0",
          searchObject: JSON.stringify([{ field: 'TO_ROW_GUID', value: this.rowData.ROW_GUID }]),
          token: window.localStorage.getItem('token')
        }
      }
      const res = await FileServerProxy(data)
      const result = JSON.parse(res.data.data).Data[0]
      this.JFData = result
      //根据window.sessionStorage.setItem('RUNTYPE', res[0].RUNTYPE == '1')
      if (window.sessionStorage.getItem('JZGSData')) {
        this.active = 2
        let JSONData = JSON.parse(window.sessionStorage.getItem('JZGSData'))
        this.JFData.BG_SGDWQYMC = JSONData.qymc
        this.JFData.BG_SGDWXYDM = JSONData.zzjgdm
      } else if (window.sessionStorage.getItem('GJdata')) {
        // window.sessionStorage.getItem('GJdata')
        this.active = 2
        let JSONData = JSON.parse(window.sessionStorage.getItem('GJdata'))
        this.JFData.BG_SGDWQYMC = JSONData.artisanName
        this.JFData.BG_SGDWXMFZR = JSONData.artisanName
      }
      if (this.JFData.BG_SGDWXYDM) {
        debugger
        this.getProgectData()
      }
      this.JFData.IF_BG = '1'
      this.JFData.BG_HZXM = result.HOUSEAPPLICANT
      this.JFData.BG_SFZH = result.CERTIFICATECODE
      this.JFData.BG_LXDH = result.APPLYPHONE
      if(this.JFData['YQCS'] == null || this.JFData['YQCS'] == 0){
        this.JFData['YQCS'] = 1
      } else if (this.JFData['YQCS'] == 1){
        this.JFData['YQCS'] = 2
      } else if (this.JFData['YQCS'] == 2){
        this.JFData['YQCS'] = 3
      }
      console.log(this.JFData, 'this.JFData.BG_SGDWXMFZR')
      this.saveDatas()
    },
    handlegotoGJ() {},
    handlegotoGJ1(data) {
      this.update()
      if (data == '施工单位') {
        this.$router.push({
          path: '/appraisalCompany',
          query: {
            id: '施工备案变更'
          }
        })
      } else {
        this.$router.push({
          path: '/craftsmanServices',
          query: {
            id: '施工备案变更'
          }
        })
      }
    }
  },
  mounted() {},
  components: { xiangqing, selectPicker, upLoad }
}
</script>

<style lang="scss" scoped>
.biangeng {
  width: 100%;
  height: 100%;
  padding: 6px;
  font-size: 14px;
  overflow: auto;

  .middle {
    h3 {
      line-height: 36px;
    }

    .p {
      line-height: 24px;
      height: 30px;
    }
  }

  .footer {
    position: fixed;
    bottom: 10px;
    right: 50%;
    transform: translateX(50%);
  }
}
</style>

<style>
.biangeng .van-field__label {
  color: black;
  width: 8em;
  font-weight: 550;
}

.biangeng .van-field__control {
  text-align: right;
}

.biangeng .van-radio--horizontal {
  margin-right: 30px;
}

.biangeng .van-cell {
  padding: 7px 8px;
}

.biangeng .van-cell--required::before {
  left: 2px;
}
</style>
